﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Data.Json;

namespace ClockLib
{
    public sealed class ClockData
    {
        public ClockData()
        {
            Clock_id = 0;
            Clock_event = "";
            Repeat_interval = "";
            Repeat_type = RepeatType.NoRepeat;
            Service_type = 0;
            Trig_time = DateTimeOffset.Now.ToUnixTimeSeconds();
        }

        public ClockData(ClockData copy)
        {
            Clock_id = copy.Clock_id;
            Clock_event = copy.Clock_event;
            Repeat_interval = copy.Repeat_interval;
            Repeat_type = copy.Repeat_type;
            Service_type = copy.Service_type;
            Trig_time = copy.Trig_time;
        }

        public JsonObject ToJsonObject()
        {
            JsonObject info = new JsonObject();
            info.SetNamedValue("clock_id", JsonValue.CreateNumberValue(Clock_id));
            info.SetNamedValue("event", JsonValue.CreateStringValue(Clock_event));
            info.SetNamedValue("trig_time", JsonValue.CreateNumberValue(Trig_time));
            info.SetNamedValue("repeat_type", JsonValue.CreateNumberValue((int)Repeat_type));
            info.SetNamedValue("repeat_interval", JsonValue.CreateStringValue(Repeat_interval));
            info.SetNamedValue("service_type", JsonValue.CreateNumberValue(Service_type));
            info.SetNamedValue("service_data", JsonValue.CreateStringValue(""));

            return info;
        }

        public void FromJsonObject(JsonObject info)
        {
            Clock_id = (long)info.GetNamedNumber("clock_id",0);
            Clock_event = info.GetNamedString("event", "");
            Trig_time = (long)info.GetNamedNumber("trig_time", DateTimeOffset.Now.ToUnixTimeSeconds());
            Repeat_type = (RepeatType)(int)info.GetNamedNumber("repeat_type", 0);
            Repeat_interval = info.GetNamedString("repeat_interval", "");
            Service_type = (int)info.GetNamedNumber("service_type", 0);
        }

        public long Clock_id { get; set; }                        // 闹钟id            
        public string Clock_event { get; set; }                     // 闹钟事件
        public string Repeat_interval { get; set; }                 // 循环间隔
        public RepeatType Repeat_type { get; set; }                        // 循环类型，1：按天循环（每天，每隔几天），2：按周循环（每周几）
        public int Service_type { get; set; }                       // 是否为定时(播放/指令)Skill，0：非定时Skill，1：定时Skill
        public long Trig_time { get; set; }                         // 闹钟触发的时间点，循环闹钟为最近一次触发时间点
        public string Repeat_interval_Text
        {
            get
            {
                switch (Repeat_type)
                {
                    case RepeatType.NoRepeat:
                        return "";
                    case RepeatType.EveryDay:
                        return "每" + Repeat_interval + "天";
                    case RepeatType.EveryWeek:
                        string ret = "每";
                        for (int i = 0; i < 7; i++)
                        {
                            if(Repeat_interval[6-i]=='1')
                            {
                                switch (i)
                                {
                                    case 0:
                                        ret += "周一,";
                                        break;
                                    case 1:
                                        ret += "周二,";
                                        break;
                                    case 2:
                                        ret += "周三,";
                                        break;
                                    case 3:
                                        ret += "周四,";
                                        break;
                                    case 4:
                                        ret += "周五,";
                                        break;
                                    case 5:
                                        ret += "周六,";
                                        break;
                                    case 6:
                                        ret += "周日,";
                                        break;
                                    default:
                                        break;
                                }
                            }
                        }
                        foreach (var item in new SortedSet<string>(Repeat_interval.Split(",")))
                        {
                            int index = 0;
                            if (int.TryParse(item, out index))
                            {
                                switch (index)
                                {
                                    case 1:
                                        ret += "周一,";
                                        break;
                                    case 2:
                                        ret += "周二,";
                                        break;
                                    case 3:
                                        ret += "周三,";
                                        break;
                                    case 4:
                                        ret += "周四,";
                                        break;
                                    case 5:
                                        ret += "周五,";
                                        break;
                                    case 6:
                                        ret += "周六,";
                                        break;
                                    case 7:
                                        ret += "周日,";
                                        break;
                                    default:
                                        break;
                                }
                            }
                        }
                        ret = ret.Remove(ret.Length - 1);
                        return ret;
                    default:
                        return "";
                }
            }
        }
        public DateTimeOffset DateTime
        {
            get
            {
                return DateTimeOffset.FromUnixTimeSeconds(Trig_time).ToLocalTime();
            }
            set
            {
                Trig_time = value.ToUnixTimeSeconds();
            }
        }
        public string Date
        {
            get
            {
                return DateTime.LocalDateTime.Date.ToShortDateString();
            }
        }
        public string Time
        {
            get
            {
                return DateTime.LocalDateTime.TimeOfDay.ToString();
            }
        }
    }
}
